package com.kiln.juxingqs.ui

import android.content.Intent
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.ZrLots2Adapter
import com.kiln.juxingqs.api.bean.GetDddBatchRet
import com.kiln.juxingqs.databinding.ActivityZrLots2Binding
import com.kiln.juxingqs.dialog.MsgTipTitleDialog
import com.kiln.juxingqs.ui.vm.ZrLotsViewModel

/**
 * 商品2-明细(释放商品)
 */
class ZrLots2Activity : BaseMiddleActivity<ZrLotsViewModel, ActivityZrLots2Binding>() {

    override fun getLayout(): Int {
        return R.layout.activity_zr_lots2
    }

    var goods_id: String? = null
    var num: String? = null
    var goods_name: String? = null

    val adapter: ZrLots2Adapter by lazy {
        ZrLots2Adapter(this)
    }

    override fun initViewModel(): ZrLotsViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel

        goods_id = intent.getStringExtra("goods_id")
        goods_name = intent.getStringExtra("goods_name")
        num = intent.getStringExtra("num")

        val decoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        val dividerDrawable = resources.getDrawable(R.drawable.drawable_divider)
        decoration.setDrawable(dividerDrawable)
        binding.recyclerView.addItemDecoration(decoration)
        binding.recyclerView.adapter = adapter

        binding.refreshLayout.setOnRefreshListener {
            adapter.hasMoreState.value = true
            viewModel.pageOne.value = 0
            viewModel.holdOne(goods_id, "2")
        }
        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.holdOne(goods_id, "2")
            }
        }

        binding.emptyLayout.setOnClickListener {
            adapter.hasMoreState.value = true
            viewModel.pageOne.value = 0
            viewModel.holdOne(goods_id, "2")
        }
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })

        binding.tvColumn4.layoutParams = LinearLayout.LayoutParams(
            0,
            LinearLayout.LayoutParams.MATCH_PARENT,
            1.2f
        )

    }

    override fun initData() {
        viewModel.pageOne.value = 0
        viewModel.holdOne(goods_id, "2")
    }

    override fun observerUI() {
        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.batch_list as MutableList<GetDddBatchRet.Batch?>
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 20) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })

    }


    fun oneClickSave() {
        MsgTipTitleDialog(this).apply {
            tip = "一键转存"
            title = "是否确认转存"
            okTxt = "确定"
            cancleTxt = "取消"
            ok = {
                viewModel.saveTo()
            }
        }.show()
    }

    //商品一:20 商品二:21 特价券:22 可交易商品:23
    fun detail() {
        startActivity(Intent(this, XyzrNewActivity::class.java).apply {
            putExtra("detail_type", "21")
            putExtra("goods_id", goods_id)//新goods_id
        })
    }


}